<?php
class EducationController extends Zend_Controller_Action
{
	protected $db; // 数据库指针

	// 初始化
	function init()
	{
		// 取得数据库指针
		$this->db=Zend_Registry::get('database');
		$this->view->action=$this->_getParam('action');
		$this->view->controller=$this->_getParam('controller');
	}

	// 处理no action异常
	function __call($method, $args)
	{
		if ('Action'==substr($method, -6)) {
			$this->view->action=substr($method, 0, -6);
			$this->view->param=$this->_getAllParams();
			$this->renderScript('error/no-action.php');
		}
		else // 其他异常
			$this->_redirect('/');
	}

	/* 下面是action处理函数: ***************************/

	// 首页
	function indexAction()
	{
		$this->view->title='教学信息－软件教研室';

		$table=new course;
		$this->view->data=$table->all();
	}

	// 课程详情
	function courseAction()
	{
		$edu=new course;
		$cid=intval($this->_getParam('CId'));
		$data = $edu->fetch($cid);
		$this->view->course = $edu->format($data);
		$this->view->teacher = $edu->teacher($data['Teacher']);
		$this->view->ware = $edu->courseware(0, $cid);
		$this->view->homework = $edu->homework(0, $cid);
		$this->view->title='课程：'.$data['Name'].'－软件教研室';
	}

	// 增加或修改课程对话框
	function addAction()
	{
		$table=new account();
		$this->view->teacher=array();
		$table->teacher(&$this->view->teacher);

		if ($this->_hasParam('CId')) {
			$this->view->cid=intval($this->_getParam('CId'));
			$edu=new course;
			$this->view->data=$edu->fetch($this->view->cid);
			$this->view->title=$this->view->data['Name'].'－修改';
		}
	}

	// 增加或修改课程
	function updateAction()
	{
		// authority check
		if (!auth::check(auth::EDIT_LAB_STUFF)) {
			$this->view->msg='你的权限不够。';
			$this->renderScript('error/msg.php');
			return;
		}

		$param=$this->_getAllParams();
		$cid=intval($this->_getParam('CId'));
		if (empty($cid))
			unset($param['CId']);
		$table=new course;
		$result=$table->check($param);
		if ($result==-1) {
			$this->view->msg='缺少参数 "'.$table->err_field().'" 。';
			$this->renderScript('error/msg.php');
			return;
		}
		else if ($result==-2) {
			$this->view->msg='参数 "'.$table->err_field().'" 不正确。';
			$this->renderScript('error/msg.php');
			return;
		}

		if (empty($cid)) // add
			$this->view->cid=$table->add($result);
		else { // update
			$result=$table->update($result);
			if ($result)
				$this->view->cid=$cid;
			else
				$this->view->cid=-1;
		} // end of if

	}

	// 删除一门课程
	function delAction()
	{
		// authority check
		if (!auth::check(auth::EDIT_LAB_STUFF)) {
			$this->view->msg='你的权限不够。';
			$this->renderScript('error/msg.php');
			return;
		}

		$this->_helper->viewRenderer->setNoRender();
		$cid=intval($this->_getParam('CId'));
		$table=new course;
		$result=$table->del($cid);
		echo "$result";
	}

	// 增加课件对话框
	function addwareAction()
	{
		$cid = $this->_getParam('CId');
		$this->view->cid = intval($cid);
	}

	// 增加课件
	function coursewareAction()
	{
		// authority check
		if (!auth::check(auth::EDIT_LAB_STUFF)) {
			$this->view->msg='你的权限不够。';
			$this->renderScript('error/msg.php');
			return;
		}

		$cid = $this->_getParam('CId');
		if (empty($cid)) {
			$this->view->msg='出现错误。空的课程CId。';
			$this->renderScript('error/msg.php');
			return;
		}
		$uploader = auth::name();
		$title = $this->_getParam('Name');
		if (strlen($title)<1)
			$title = "untitled_".time();

		$this->view->result = upload::courseware('file', 
							$title, $uploader, $cid, $this->db);
	}

	// 增加作业对话框
	function addhomeworkAction()
	{
		$cid = $this->_getParam('CId');
		$this->view->cid = intval($cid);
	}

	// 增加作业
	function homeworkAction()
	{
		// authority check
		if (!auth::check(auth::EDIT_LAB_STUFF)) {
			$this->view->msg='你的权限不够。';
			$this->renderScript('error/msg.php');
			return;
		}

		$cid = $this->_getParam('CId');
		if (empty($cid)) {
			$this->view->msg='出现错误。空的课程CId。';
			$this->renderScript('error/msg.php');
			return;
		}
		$param = $this->_getAllParams();

		$edu = new course;
		$this->view->result = $edu->add_homework($cid, $param);
	}

	// 删除一个课件
	function delwareAction()
	{
		// authority check
		if (!auth::check(auth::EDIT_LAB_STUFF)) {
			$this->view->msg='你的权限不够。';
			$this->renderScript('error/msg.php');
			return;
		}

		$this->_helper->viewRenderer->setNoRender();
		$wid=intval($this->_getParam('WId'));
		$table=new course;
		$result=$table->del_courseware($wid, 0);
		echo $result;
	}
	
	// 删除一个作业
	function delworkAction()
	{
		// authority check
		if (!auth::check(auth::EDIT_LAB_STUFF)) {
			$this->view->msg='你的权限不够。';
			$this->renderScript('error/msg.php');
			return;
		}

		$this->_helper->viewRenderer->setNoRender();
		$hid=intval($this->_getParam('HId'));
		$table=new course;
		$result=$table->del_homework($hid, 0);
		echo $result;
	}

	// 显示作业
	function showhomeworkAction()
	{
		$table=new course;
		$hid = $this->_getParam('HId');
		$result = $table->homework($hid, 0);
		$this->view->data = $table->format($result[0]);
	}

	// 编译原理课
	function compilerAction()
	{
		$this->view->title = '编译原理精品课－软件教研室';
		if ($this->_hasParam('page'))
			$this->view->file = $this->_getParam('page');
		else
			$this->view->file = 'compiler1.htm';
	}

	// 高级程序设计课
	function programmingAction()
	{
		$this->view->title = '高级程序设计精品课－软件教研室';
	}

};
?>
